package com.origin.niuke.tree;

import general_class.TreeNode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/5/11 22:52
 * 二叉树的最大深度
 * 算法：深度优先搜索、广度优先遍历
 */
public class NC13 {

    public int maxDepth (TreeNode root) {
        // write code here
        if (root == null) return 0;
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        int ans = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                root = queue.poll();
                if (root.left != null) queue.offer(root.left);
                if (root.right != null) queue.offer(root.right);
            }
            ++ans;
        }
        return ans;
    }

    int maxDepth = 0;
    public void dfs(TreeNode root, int depth) {
        if (root == null) return;
        ++depth;
        if (root.left == null && root.right == null) {
            maxDepth = Math.max(maxDepth, depth);
            return;
        }
        dfs(root.left, depth);
        dfs(root.right, depth);
    }

}
